/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Initialising.java
 *
 * Created on Oct 29, 2012, 5:10:08 PM
 */
package test.shanika;

import camera.Discovering;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import java.awt.Desktop.Action;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import java.awt.AWTException;
import java.awt.Robot;
import com.googlecode.javacv.cpp.opencv_calib3d.*;
import com.googlecode.javacv.FrameGrabber.Exception;
import java.util.logging.Level;
import java.util.logging.Logger;
import static com.googlecode.javacv.cpp.opencv_core.*;
import com.googlecode.javacv.*;
import com.googlecode.javacv.cpp.opencv_calib3d.*;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import javax.swing.Timer;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;

/**
 *
 * @author Home
 */
public class Initialising extends javax.swing.JFrame {

    /** Creates new form Initialising */
    public Initialising() {
        initComponents();
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    public static IplImage[] imfromcam = new IplImage[4];
    Timer tt1;
    IplImage tempIm = cvCreateImage(new CvSize(257, 185), 8, 3);
    IplImage tempIm1 = cvCreateImage(new CvSize(257, 185), 8, 3);
    IplImage tempIm2 = cvCreateImage(new CvSize(257, 185), 8, 3);
    IplImage tempIm3 = cvCreateImage(new CvSize(257, 185), 8, 3);

     public static void setimfromcam(IplImage img,int number){
        imfromcam[number]= img;
    }



    AbstractAction explcam = new AbstractAction() {

        public void actionPerformed(ActionEvent e) {

            for (int i = 0; i < imfromcam.length; i++) {
            if (imfromcam[i] !=null) {
                im[i] = 1;

            } else {
                im[i] = 0;
            }
            System.out.println("im["+i+"] ="+im[i]);

        }
            ImageIcon[] ic1 = new ImageIcon[4];
            for (int i = 0; i < imfromcam.length; i++) {
                ic1[i] = new ImageIcon();
                if (im[i] != 0) {
                    try {
                        if(i==0){
                        cvResize(imfromcam[i], tempIm);
                        ic1[i].setImage(tempIm.getBufferedImage());
                        }
                        if(i==1){
                        cvResize(imfromcam[i], tempIm1);
                        ic1[i].setImage(tempIm1.getBufferedImage());
                        }
                        if(i==2){
                        cvResize(imfromcam[i], tempIm2);
                        ic1[i].setImage(tempIm2.getBufferedImage());
                        }
                        if(i==3){
                        cvResize(imfromcam[i], tempIm3);
                        ic1[i].setImage(tempIm3.getBufferedImage());
                        }
                    } catch (NullPointerException ex) {
                        System.out.println("null image from camera no " + i);
                    }
                }

            }
            if(im[0]!=0)
              c0.setIcon(ic1[0]);

             if(im[1]!=0)
              c1.setIcon(ic1[1]);

            if(im[2]!=0)
              c2.setIcon(ic1[2]);

            if(im[3]!=0)
              c3.setIcon(ic1[3]);

            jPanel2.repaint();


        }
    };
    int[] im = new int[4];
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jPanel1 = new javax.swing.JPanel();
        findCameraB = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        c0 = new javax.swing.JLabel();
        c3 = new javax.swing.JLabel();
        c2 = new javax.swing.JLabel();
        c1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setAlwaysOnTop(true);

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jPanel1, org.jdesktop.beansbinding.ELProperty.create("${Cameras}"), this, org.jdesktop.beansbinding.BeanProperty.create("title"));
        bindingGroup.addBinding(binding);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Cameras"));

        findCameraB.setText("Find Cameras");
        findCameraB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findCameraBActionPerformed(evt);
            }
        });

        c0.setBorder(javax.swing.BorderFactory.createTitledBorder("Camera 0"));

        c3.setBorder(javax.swing.BorderFactory.createTitledBorder("Camera 3"));

        c2.setBorder(javax.swing.BorderFactory.createTitledBorder("Camera 2"));

        c1.setBorder(javax.swing.BorderFactory.createTitledBorder("Camera1"));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(c2, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(c0, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(46, 46, 46)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(c1, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(c3, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(c1, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(c3, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(c0, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(c2, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(jList1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(findCameraB, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(69, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(96, 96, 96)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addComponent(findCameraB)
                .addContainerGap(142, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void findCameraBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_findCameraBActionPerformed
        // TODO add your handling code here:
        for (int i = 0; i < 4; i++) {
            Discovering ds = new Discovering(i);
            new Thread(ds).start();
        }


        tt1 = new Timer(15, explcam);
        tt1.start();
    }//GEN-LAST:event_findCameraBActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Initialising().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel c0;
    private javax.swing.JLabel c1;
    private javax.swing.JLabel c2;
    private javax.swing.JLabel c3;
    private javax.swing.JButton findCameraB;
    private javax.swing.JList jList1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
